package com.idp.web.test.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.idp.common.persistence.Page;
import com.idp.web.test.dao.DetailListDao;
import com.idp.web.test.dao.ProductDao;
import com.idp.web.test.dao.PutDao;
import com.idp.web.test.dao.StockDao;
import com.idp.web.test.entity.DetailList;
import com.idp.web.test.entity.Product;
import com.idp.web.test.entity.Put;
import com.idp.web.test.entity.Stock;
import com.idp.web.test.service.PutService;

/**
 * 
 * 11111service实现类
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Service("putService")
@Transactional
public class PutServiceImpl implements PutService {

	@Resource
	private PutDao putDao;
	@Resource
	private DetailListDao detailListDao;
	@Resource
	private StockDao stockDao;
	@Resource
	private ProductDao productDao;

	@Override
	public Page<Put> findByPage(Put put, Page<Put> page) {
		page.setResult(putDao.find(put, page));

		return page;
	}

	@Override
	public List<Put> findBySearch(Put put) {

		return putDao.find(put);
	}

	@Override
	public Put getById(Integer id) {

		return putDao.getById(id);
	}

	// @Override
	// public void add(Put put, DetailList detailList, Stock stock) {
	// putDao.add(put);
	// if (detailList != null) {
	// detailList.setPutId(put.getId());
	// detailListDao.add(detailList);
	// }
	// if (stock.getId() != null)
	// stockDao.update(stock);
	// else
	// stockDao.add(stock);
	// }

	@Override
	public void add(Put put) {
		Product product = productDao.getById(put.getProductId());
		putDao.add(put);
		if (product.getProductStatusId() == 2) { // 2 -> 缺货
			product.setProductStatusId(4); // 4 -> 入库审核
			productDao.updateNotNull(product);
		}
	}

	@Override
	public void update(Put put) {
		putDao.update(put);
	}

	@Override
	public void delete(Integer id) {
		putDao.delete(id);
	}

	@Override
	public Page<Put> sreachByWarehouseAndPutStatus(Put put, String productName, Page<Put> page) {
		Map<String, Object> map = new HashMap<>();
		map.put("put", put);
		map.put("productName", productName);
		page.setResult(putDao.sreachByWarehouseAndPutStatus(map, page));
		return page;
	}

	@Override
	public int makeSurePut(Integer[] ids) {
		int result = 0;
		Put put = null;
		Map<String, Integer> map = new HashMap<>();
		Stock stock = null;
		Date date = new Date();
		DetailList detailList = new DetailList();
		for (Integer id : ids) {
			map.clear();
			put = putDao.getById(id);
			map.put("productId", put.getProductId());
			map.put("warehouseId", put.getWarehouseId());
			stock = stockDao.getByProductAndWarehouse(map);
			try {
				if (stock == null) {
					stock = new Stock();
					stock.setProductId(put.getProductId());
					stock.setWarehouseId(put.getWarehouseId());
					stock.setStockNum(put.getRealbox());
					result = stockDao.add(stock);
					if (result == 0)
						break;
				} else {
					stock.setStockNum(stock.getStockNum() + put.getRealbox());
					result = stockDao.update(stock);
					if (result == 0)
						break;
				}
				// 已确认
				put.setPutStatusId(4);
				put.setDatetime(date);
				result = putDao.update(put);
				if (result == 0) {
					break;
				}
				
				if (put.getProduct().getProductStatusId() != 1) {
					put.getProduct().setProductStatusId(1); // 修改入库商品的状态(1->在库)
					result = productDao.update(put.getProduct());
					if (result == 0) {
						break;
					}
				}
				// 新增入库记录
				detailList.setPutId(put.getId());
				detailList.setMount(stock.getStockNum());
				detailList.setDetailListTypeId(1);
				result = detailListDao.add(detailList);
				if (result == 0) {
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public List<Put> findByMap(Map<String, Object> map) {
		return putDao.findByMap(map);
	}

	@Override
	public void updateNotNull(Put put) {
		putDao.updateNotNull(put);
	}
}